// 错题本页面逻辑
Page({
  data: {
    totalWrong: 0,
    reviewedCount: 0,
    masteredCount: 0,
    currentCategory: 'all',
    wrongQuestions: [],
    filteredQuestions: [],
    loading: false,
    hasMore: true,
    page: 1,
    pageSize: 10
  },

  onLoad: function (options) {
    this.loadWrongQuestions();
    this.updateStatistics();
  },

  onShow: function () {
    // 页面显示时刷新数据
    this.loadWrongQuestions();
  },

  // 加载错题数据
  loadWrongQuestions: function () {
    const wrongQuestions = wx.getStorageSync('wrongQuestions') || [];
    this.setData({
      wrongQuestions: wrongQuestions,
      filteredQuestions: this.filterQuestions(wrongQuestions, this.data.currentCategory)
    });
    this.updateStatistics();
  },

  // 更新统计数据
  updateStatistics: function () {
    const questions = this.data.wrongQuestions;
    const totalWrong = questions.length;
    const reviewedCount = questions.filter(q => q.reviewed).length;
    const masteredCount = questions.filter(q => q.mastered).length;

    this.setData({
      totalWrong: totalWrong,
      reviewedCount: reviewedCount,
      masteredCount: masteredCount
    });
  },

  // 筛选题目
  filterQuestions: function (questions, category) {
    if (category === 'all') {
      return questions;
    }
    return questions.filter(q => q.category === category);
  },

  // 设置分类筛选
  setCategory: function (e) {
    const category = e.currentTarget.dataset.category;
    this.setData({
      currentCategory: category,
      filteredQuestions: this.filterQuestions(this.data.wrongQuestions, category)
    });
  },

  // 开始复习
  startReview: function () {
    const questions = this.data.filteredQuestions;
    if (questions.length === 0) {
      wx.showToast({
        title: '暂无错题可复习',
        icon: 'none'
      });
      return;
    }

    wx.navigateTo({
      url: '/pages/review/review?type=wrong'
    });
  },

  // 清空错题
  clearWrongQuestions: function () {
    wx.showModal({
      title: '确认清空',
      content: '确定要清空所有错题吗？此操作不可恢复。',
      success: (res) => {
        if (res.confirm) {
          wx.removeStorageSync('wrongQuestions');
          this.setData({
            wrongQuestions: [],
            filteredQuestions: []
          });
          this.updateStatistics();
          
          wx.showToast({
            title: '已清空错题',
            icon: 'success'
          });
        }
      }
    });
  },

  // 跳转到题目详情
  goToQuestion: function (e) {
    const id = e.currentTarget.dataset.id;
    
    // 查找题目的分类和专题信息
    const { questionCategories } = require('../../utils/questions.js');
    let categoryId = '';
    let topicId = '';
    
    for (const category of questionCategories) {
      for (const topic of category.topics) {
        for (const question of topic.questions) {
          if (question.id == id) {
            categoryId = category.id;
            topicId = topic.id;
            break;
          }
        }
        if (categoryId) break;
      }
      if (categoryId) break;
    }
    
    if (categoryId && topicId) {
      wx.navigateTo({
        url: `/pages/detail/detail?categoryId=${categoryId}&topicId=${topicId}&questionId=${id}`
      });
    } else {
      wx.showToast({
        title: '题目信息获取失败',
        icon: 'error'
      });
    }
  },

  // 标记为已掌握
  markAsMastered: function (e) {
    const id = e.currentTarget.dataset.id;
    const questions = this.data.wrongQuestions;
    const index = questions.findIndex(q => q.id === id);
    
    if (index !== -1) {
      questions[index].mastered = !questions[index].mastered;
      
      wx.setStorageSync('wrongQuestions', questions);
      this.setData({
        wrongQuestions: questions,
        filteredQuestions: this.filterQuestions(questions, this.data.currentCategory)
      });
      this.updateStatistics();
      
      wx.showToast({
        title: questions[index].mastered ? '已标记掌握' : '已取消掌握',
        icon: 'success'
      });
    }
  },

  // 从错题本移除
  removeFromWrong: function (e) {
    const id = e.currentTarget.dataset.id;
    wx.showModal({
      title: '确认移除',
      content: '确定要从错题本中移除这道题吗？',
      success: (res) => {
        if (res.confirm) {
          const questions = this.data.wrongQuestions.filter(q => q.id !== id);
          
          wx.setStorageSync('wrongQuestions', questions);
          this.setData({
            wrongQuestions: questions,
            filteredQuestions: this.filterQuestions(questions, this.data.currentCategory)
          });
          this.updateStatistics();
          
          wx.showToast({
            title: '已移除',
            icon: 'success'
          });
        }
      }
    });
  },

  // 跳转到练习页面
  goToPractice: function () {
    wx.switchTab({
      url: '/pages/practice/practice'
    });
  },

  // 下拉刷新
  onPullDownRefresh: function () {
    this.loadWrongQuestions();
    wx.stopPullDownRefresh();
  },

  // 分享功能
  onShareAppMessage: function () {
    return {
      title: '我的错题本 - 编程题库',
      path: '/pages/wrong-questions/wrong-questions'
    };
  }
}); 